public class MergeSort<T extends java.lang.Comparable<? super T>> extends TestTimes implements SortInterface<T>
TestTimesInterface.MemoryUnits, TestTimesInterface.TimeUnits| Constructor and Description |
|---|
MergeSort() |
| Modifier and Type | Method and Description |
|---|---|
private void |
merge(T[] array,
java.lang.Object[] tempArray,
int first,
int mid,
int last)
This method is called to merge two portions of
array that are already in
sorted order. |
private void |
mergesort(T[] array,
java.lang.Object[] tempArray,
int first,
int last)
This is the recursive method that does the merge sort of
array, which
has Objects of type T. |
void |
sort(T[] arrayToSort)
This is the method that users will call to sort the specified array of
Objects of type
T. |
addTestTime, getAverageMemoryUsage, getAverageTestTime, getLastMemoryUsage, getLastTestTime, getMemoryUnits, getMemoryUsages, getTestTimes, getTimeUnits, resetTestTimes, setMemoryUnits, setTimeUnitspublic void sort(T[] arrayToSort)
This is the method that users will call to sort the specified array of
Objects of type T.
This method creates a tempArray and initializes it to contain
arrayToSort.size() null elements.
This method will call the mergesort method to perform the recursive sort.
The Comparable interface must either be implemented by T
or a superclass of T.
When this method is done, the specified array is sorted.
This method will measure the runtime of the sort and add it to the RunTime class.
sort in interface SortInterface<T extends java.lang.Comparable<? super T>>arrayToSort - The array of Objects of type T to be sorted.private void mergesort(T[] array, java.lang.Object[] tempArray, int first, int last)
array, which
has Objects of type T.array - The array of Objects of type T to be sortedtempArray - The temporary array of Objects used in the mergefirst - Index of the first entry of the arraylast - Index of the last entry of the arrayprivate void merge(T[] array, java.lang.Object[] tempArray, int first, int mid, int last)
This method is called to merge two portions of array that are already in
sorted order. The indices of these portions are specified by first,
mid and last. All the elements of array are Objects
of type T.
The merge operation first merges all the elements in proper sorted order from the two portions
of array into tempArray. When the merge is complete, all the
elements are then copied back into array.
array - The array of Objects of type T to be sortedtempArray - The temporary array of Objects used in the mergefirst - The index of the beginning of the first section of array to be merged.mid - The index of the end of the first section of array to be merged.last - The index of the end of the second section of array to be merged.
Please note that the index of the beginning of the second section of array
to be merged is at index = mid + 1